package com.whiteblue.config;

import com.jfinal.config.*;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.whiteblue.Interceptor.GlobalInterceptor;
import com.whiteblue.beetl.BeetlRenderFactory;
import com.whiteblue.controller.BookController;
import com.whiteblue.controller.IndexController;
import com.whiteblue.controller.UserController;
import com.whiteblue.model.*;

/**
 * Created by WhiteBlue on 15/4/4.
 */
public class Config extends JFinalConfig {
    @Override
    public void configConstant(Constants constants) {
        constants.setDevMode(false);
        constants.setMainRenderFactory(new BeetlRenderFactory());
//        constants.setError404View("/error404.html");
//        constants.setError500View("/error500.html");
    }


    @Override
    public void configRoute(Routes routes) {
        routes.add("/", IndexController.class).add("/user", UserController.class);
        routes.add("/book", BookController.class);
    }

    @Override
    public void configPlugin(Plugins plugins) {
        String jdbcUrl = PropKit.use("config.txt").get("SQL_URL");
        String user = PropKit.use("config.txt").get("SQL_USER");
        String password = PropKit.use("config.txt").get("SQL_PW");

        C3p0Plugin cp = new C3p0Plugin(jdbcUrl, user, password);
        plugins.add(cp);
        ActiveRecordPlugin arp = new ActiveRecordPlugin(cp);
        arp.setShowSql(false);
        plugins.add(arp);
        plugins.add(new EhCachePlugin());

        arp.addMapping("b_user", User.class).addMapping("b_book", Book.class).
                addMapping("b_category", Category.class).addMapping("b_post", Post.class)
                .addMapping("b_sort", Sort.class);
    }

    @Override
    public void configInterceptor(Interceptors interceptors) {
        interceptors.add(new GlobalInterceptor());
    }

    @Override
    public void configHandler(Handlers handlers) {
        handlers.add(new ContextPathHandler("base"));
    }
}
